package qeb.handheldmedical;

//import alert_dialog.dialog_types;

import java.text.BreakIterator;

import qeb.handheldmedical.R;
import qeb.handheldmedical.dialog.MultiDialog;
import qeb.handheldmedical.model.User;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends Activity {
	// View
	private TextView tv_title;
	private TextView tv_main;
	private TextView tv_tool;
	private TextView tv_set;
	private ImageView iv_mm_zybr, iv_mm_mzbr, iv_mm_cybr, iv_mm_brcx,
			img_mm_sssp, iv_mm_ssap, iv_mm_hzqq, iv_mm_hzhf, iv_mm_sbsh,
			iv_mm_learnig, img_mm_yhinfo;
	private TextView txt_sbsh;
	private Button btn_title_back;
	private View v1, v2;
	private TableLayout tl_mm_main, tl_mm_tool;

	private User usr = new User();
	private String isApply = "0";

	private GestureDetector gestureDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sys_home);
		initView();
	}

	private void initView() {
		tv_title = (TextView) findViewById(R.id.tv_top_title);
		tv_title.setText("掌上医疗");
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(sysMenuOnClickListener);
		tv_main = (TextView) findViewById(R.id.tv_main);
		tv_main.setOnClickListener(sysMenuOnClickListener);
		tv_tool = (TextView) findViewById(R.id.tv_tool);
		tv_tool.setOnClickListener(sysMenuOnClickListener);
		tv_set = (TextView) findViewById(R.id.tv_out);
		tv_set.setOnClickListener(sysMenuOnClickListener);
		v1 = (View) findViewById(R.id.iv_cursor1);
		v2 = (View) findViewById(R.id.iv_cursor2);
		tv_main = (TextView) findViewById(R.id.tv_main);
		tv_tool = (TextView) findViewById(R.id.tv_tool);
		tl_mm_main = (TableLayout) findViewById(R.id.tl_mm_main);
		tl_mm_tool = (TableLayout) findViewById(R.id.tl_mm_tool);
		usr = (User) getIntent().getSerializableExtra("userinfo");
		isApply = String.valueOf(getIntent().getSerializableExtra("isApply"));
		isApply = isApply.equals("1") ? "1" : "0";
		iv_mm_zybr = (ImageView) findViewById(R.id.img_mm_zybr);
		iv_mm_zybr.setOnClickListener(MyOnClickListener);
		iv_mm_mzbr = (ImageView) findViewById(R.id.img_mm_mzbr);
		iv_mm_mzbr.setOnClickListener(MyOnClickListener);
		iv_mm_cybr = (ImageView) findViewById(R.id.img_mm_cybr);
		iv_mm_cybr.setOnClickListener(MyOnClickListener);
		iv_mm_brcx = (ImageView) findViewById(R.id.img_mm_brcx);
		iv_mm_brcx.setOnClickListener(MyOnClickListener);

		iv_mm_hzqq = (ImageView) findViewById(R.id.img_mm_hzqq);
		iv_mm_hzqq.setOnClickListener(MyOnClickListener);

		iv_mm_hzhf = (ImageView) findViewById(R.id.img_mm_hzhf);
		iv_mm_hzhf.setOnClickListener(MyOnClickListener);

		iv_mm_ssap = (ImageView) findViewById(R.id.img_mm_ssap);
		iv_mm_ssap.setOnClickListener(MyOnClickListener);

		img_mm_sssp = (ImageView) findViewById(R.id.img_mm_sssp);
		img_mm_sssp.setOnClickListener(MyOnClickListener);

		iv_mm_sbsh = (ImageView) findViewById(R.id.img_mm_sbsh);
		txt_sbsh = (TextView) findViewById(R.id.txt_sbsh);

		// 用户信息
		img_mm_yhinfo = (ImageView) findViewById(R.id.img_mm_yhinfo);
		img_mm_yhinfo.setOnClickListener(MyOnClickListener);
		// 在线学习
		iv_mm_learnig = (ImageView) findViewById(R.id.img_mm_learning);
		iv_mm_learnig.setOnClickListener(MyOnClickListener);
		if (usr.GH.equals("admin")) {
			iv_mm_sbsh.setOnClickListener(MyOnClickListener);
		} else {
			iv_mm_sbsh.setVisibility(ImageView.INVISIBLE);
			txt_sbsh.setText("");
		}
		gestureDetector = new GestureDetector(Home.this, onGestureListener);
	}

	private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			float x = e2.getX() - e1.getX();
			if (x < 0) {
				tv_main.setTextColor(Color.parseColor("#cccccc"));
				tv_tool.setTextColor(Color.parseColor("#ffffffff"));
				v1.setBackgroundColor(Color.parseColor("#ffffffff"));
				v2.setBackgroundColor(Color.parseColor("#cccccc"));
				tl_mm_main.setVisibility(TableLayout.VISIBLE);
				tl_mm_tool.setVisibility(TableLayout.GONE);
			} else if (x > 0) {
				tv_tool.setTextColor(Color.parseColor("#cccccc"));
				tv_main.setTextColor(Color.parseColor("#ffffffff"));
				v1.setBackgroundColor(Color.parseColor("#cccccc"));
				v2.setBackgroundColor(Color.parseColor("#ffffffff"));
				tl_mm_tool.setVisibility(TableLayout.VISIBLE);
				tl_mm_main.setVisibility(TableLayout.GONE);
			}
			return true;
		}
	};

	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}

	/* 系统主菜单点击事件（在院病人、门诊病人等） */
	private OnClickListener MyOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			// Bundle bundle=new Bundle();
			if (!isApply.equals("1")) {
				final MultiDialog mydialog = new MultiDialog(Home.this,
						MultiDialog.DialogType.ALERT);
				mydialog.setMessage("该手机尚未认证，请联系信息科！");
				mydialog.show();
			} else {
				switch (v.getId()) {
				case R.id.img_mm_zybr:// 在院病人图片按钮
					// TODO Auto-generated method stub
					final Intent intent_zybr = new Intent(Home.this,
							qeb.handheldmedical.patient.InPatientList.class);
					intent_zybr.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent_zybr.putExtra("userinfo", usr);
					intent_zybr.putExtra("patientstate", "1");
					Home.this.getApplicationContext()
							.startActivity(intent_zybr);
					break;
				case R.id.img_mm_mzbr:// 门诊病人图片按钮
					final Toast tt_mzbr = Toast.makeText(Home.this,
							"门诊功能建设中，谢谢您的关注！", Toast.LENGTH_LONG);
					tt_mzbr.show();
					break;
				case R.id.img_mm_cybr:// 出院病人图片按钮
					/*
					 * final Intent intent_zndz = new Intent(Home.this,
					 * qeb.handheldmedical.zndz.BrowserActivity.class);
					 * intent_zndz.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					 * intent_zndz.putExtra("userinfo", usr);
					 * 
					 * Home.this.getApplicationContext()
					 * .startActivity(intent_zndz);
					 */
					break;
				case R.id.img_mm_brcx:// 出院病人图片按钮
					final Intent intent_brcx = new Intent(Home.this,
							qeb.handheldmedical.patient.InPatientList.class);
					intent_brcx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent_brcx.putExtra("userinfo", usr);
					Home.this.getApplicationContext()
							.startActivity(intent_brcx);
					break;
				case R.id.img_mm_jybg:// 门诊病人图片按钮
					final Toast tt_jybg = Toast.makeText(Home.this,
							"功能建设中，谢谢您的关注！", Toast.LENGTH_LONG);
					tt_jybg.show();
					break;
				case R.id.img_mm_jcbg:// 门诊病人图片按钮
					final Toast tt_jcbg = Toast.makeText(Home.this,
							"功能建设中，谢谢您的关注！", Toast.LENGTH_LONG);
					tt_jcbg.show();
					break;
				case R.id.img_mm_hzqq:// 会诊请求图片按钮
					final Intent intent_hzqq = new Intent(
							Home.this,
							qeb.handheldmedical.patient.ConsultationQQList.class);
					intent_hzqq.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent_hzqq.putExtra("userinfo", usr);
					Home.this.getApplicationContext()
							.startActivity(intent_hzqq);
					break;
				case R.id.img_mm_hzhf:// 会诊回复图片按钮
					// TODO Auto-generated method stub
					final Intent intent_hzhf = new Intent(
							Home.this,
							qeb.handheldmedical.patient.ConsultationDfList.class);
					intent_hzhf.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent_hzhf.putExtra("userinfo", usr);
					Home.this.getApplicationContext()
							.startActivity(intent_hzhf);
					break;
				case R.id.img_mm_ssap:// 手术安排图片按钮
					// TODO Auto-generated method stub
					final Intent intent_ssap = new Intent(
							Home.this,
							qeb.handheldmedical.patient.OperationListQuery.class);
					intent_ssap.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent_ssap.putExtra("userinfo", usr);
					Home.this.getApplicationContext()
							.startActivity(intent_ssap);
					break;
				case R.id.img_mm_sssp:// 手术审批
					final Intent intent_sssp = new Intent(Home.this,
							qeb.handheldmedical.approval.ApprovalMain.class);
					intent_sssp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent_sssp.putExtra("userinfo", usr);
					Home.this.getApplicationContext()
							.startActivity(intent_sssp);
					break;
				case R.id.img_mm_sbsh:// 设备审核图片按钮
					// TODO Auto-generated method stub
					final Intent intent_sbsh = new Intent(Home.this,
							qeb.handheldmedical.user.PhoneList.class);
					intent_sbsh.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent_sbsh.putExtra("userinfo", usr);
					Home.this.getApplicationContext()
							.startActivity(intent_sbsh);
					break;
				// 在线考试
				case R.id.img_mm_learning:
					final Intent intent_learning = new Intent(Home.this,
							qeb.handheldmedical.exam.LineExamMain.class);
					intent_learning.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent_learning.putExtra("userinfo", usr);
					Home.this.getApplicationContext().startActivity(
							intent_learning);
					break;
				// 用户信息
				case R.id.img_mm_yhinfo:
					final Intent intent_yhxx = new Intent(Home.this,
							qeb.handheldmedical.user.UserInfo.class);
					intent_yhxx.putExtra("userinfo", usr);

					startActivityForResult(intent_yhxx, 0);
					break;
				default:
					break;
				}
			}
		}
	};

	/* 下面主菜单点击事件（选项、功能、退出） */
	private OnClickListener sysMenuOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_title_back:// 返回按钮
				// TODO Auto-generated method stub
				final MultiDialog backdialog = new MultiDialog(Home.this,
						MultiDialog.DialogType.CONFIRM);
				backdialog.setTitle("系统提醒");
				backdialog.setMessage("该操作将返回到登录页面，是否确定继续？");
				backdialog.setDialogGoListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						final Intent intent_back = new Intent(Home.this,
								Login.class);
						intent_back.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						Home.this.getApplicationContext().startActivity(
								intent_back);
						Home.this.finish();
					}
				});
				backdialog.show();
				break;
			case R.id.tv_main:// 点击功能
				tv_main.setTextColor(Color.parseColor("#cccccc"));
				tv_tool.setTextColor(Color.parseColor("#ffffffff"));
				v1.setBackgroundColor(Color.parseColor("#ffffffff"));
				v2.setBackgroundColor(Color.parseColor("#cccccc"));
				tl_mm_main.setVisibility(TableLayout.VISIBLE);
				tl_mm_tool.setVisibility(TableLayout.GONE);
				break;
			case R.id.tv_tool:// 点击选项
				tv_tool.setTextColor(Color.parseColor("#cccccc"));
				tv_main.setTextColor(Color.parseColor("#ffffffff"));
				v1.setBackgroundColor(Color.parseColor("#cccccc"));
				v2.setBackgroundColor(Color.parseColor("#ffffffff"));
				tl_mm_tool.setVisibility(TableLayout.VISIBLE);
				tl_mm_main.setVisibility(TableLayout.GONE);
				break;
			case R.id.tv_out:// 点击选项
				final MultiDialog outdialog = new MultiDialog(Home.this,
						MultiDialog.DialogType.CONFIRM);
				outdialog.setTitle("系统提醒");
				outdialog.setMessage("是否确定退出系统？");
				outdialog.setDialogGoListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Home.this.finish();
						System.exit(0);
					}
				});
				outdialog.show();
				break;
			}
		}
	};

	/* 返回按键事件 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			final MultiDialog mdialog = new MultiDialog(Home.this,
					MultiDialog.DialogType.CONFIRM);
			mdialog.setTitle("系统提醒");
			mdialog.setMessage("是否确定退出系统？");
			mdialog.setDialogGoListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Home.this.finish();
					System.exit(0);
				}
			});
			mdialog.show();
		}
		if (keyCode == KeyEvent.KEYCODE_F9 || keyCode == KeyEvent.KEYCODE_F10
				|| keyCode == KeyEvent.KEYCODE_F11) {
			return true;

		}
		return false;
	}

	/* 关闭Activity并且退出系统时调用 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		// System.exit(0);
	}

	/*
	 * 得到返回值
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// 更新用户信息
		if (resultCode == -1) {
			usr = (User) data.getExtras().getSerializable("userinfo");// 得到新Activity关闭后返回的数据
		}
	}
}
